using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Module.CMS.Library;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library.Distributions;
using NCS.Module.CMS.Library.Definitions;

namespace NCS.Module.CMS.Web.HomePage
{
    public partial class ListNewsByTabID : NCS.Module.CMS.Library.CMSWebControl
    {
        private string groupid;
        public string GroupId
        {
            get
            {
                return groupid;
            }
            set { groupid = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (curZone == null) return;

            this.link_zone.Text = curZone.Zone_Name;
            //  this.link_zone.NavigateUrl = BuildUrlByZone(GroupId, curZone.Portal_TabControlID);

        }

        public string lblPage = "";


        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (String.IsNullOrEmpty(GroupId))
            {
                GroupId = Request["catid"].ToString();
            }
          
            int totalRecord;
            if (Request.QueryString["page"] != null)
            {
                txtHiddenValue.Text = ConvertUtility.ToString((ConvertUtility.ToInt32(Request.QueryString["page"]) - 1));
            }
            else
            {
                txtHiddenValue.Text = "0";
            }
            int pagesize = 10;
            DataTable dtContents = DistributionController.GetNewContentByZoneIDAndRank(Convert.ToInt32(GroupId), false, pagesize, ConvertUtility.ToInt32(txtHiddenValue.Text), out totalRecord);
            this.rpt_head.DataSource = dtContents;
            this.rpt_head.DataBind();

            if (dtContents != null && dtContents.Rows.Count > 0)
            {
                this.rpt_foot.DataSource = DistributionController.GetDistributedContents(Convert.ToInt32(GroupId), 10, (int)CMSDistributionRank.Special);
                this.rpt_foot.DataBind();
            }
            else
            {
                this.pal1.Visible = false;
                this.pal2.Visible = false;
                return;
            }

            this.pal1.Visible = totalRecord > pagesize;// this.rpt_head.Items.Count > 0;
            this.pal2.Visible = this.rpt_foot.Items.Count > 0;

            #region paging

            double pagenum = totalRecord % pagesize;
            if (pagenum == 0)
            {
                for (int i = 1; i < totalRecord / pagesize + 1; i++)
                {
                    if (ConvertUtility.ToInt32(txtHiddenValue.Text) == 0)
                    {
                        if (i == 1)
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                        }
                        else
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                        }
                    }
                    else if (i == ConvertUtility.ToInt32(txtHiddenValue.Text) + 1)
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                    }
                    else
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                    }
                }
            }
            else if (totalRecord % pagesize > 0)
            {
                for (int i = 1; i < totalRecord / pagesize + 2; i++)
                {
                    if (ConvertUtility.ToInt32(txtHiddenValue.Text) == 0)
                    {
                        if (i == 1)
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                        }
                        else
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                        }
                    }
                    else if (i == ConvertUtility.ToInt32(txtHiddenValue.Text) + 1)
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                    }
                    else
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                    }
                }
            }
            #endregion

        }

        protected void rpt_foot_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                if (currow != null)
                {
                    HyperLink hplTitle = (HyperLink)e.Item.FindControl("link");

                    hplTitle.Text = ConvertUtility.ToString(currow["Content_Headline"]);

                    string tooltip = MiscUtility.ToolTip(ConvertUtility.ToString(currow["Content_Headline"]), ConvertUtility.ToString(currow["Content_SubTitle"]), ConvertUtility.ToString(currow["Content_Avatar"]), ConvertUtility.ToString(currow["Content_Teaser"]), ConvertUtility.ToString(currow["Content_Alias"]), ConvertUtility.ToDateTime(currow["Distribution_CreateDate"]));
                    hplTitle.Attributes.Add("onmouseover", "showtip('" + tooltip + "','322','#EBF3FB')");
                    hplTitle.Attributes.Add("onmouseout", "hidetip()");

                    //hplTitle.NavigateUrl = BuildUrlByDistId((int)currow["Distribution_ID"], this.GroupId);
                    hplTitle.NavigateUrl = BuildUrlByDistId((int)currow["Distribution_ID"], Convert.ToInt32(GroupId));
                }
            }
        }

        protected void rpt_head_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView row = (DataRowView)e.Item.DataItem;
            if (row != null)
            {
                //Label lbalias = (Label)e.Item.FindControl("lbalias");
                //lbalias.Text = (ConvertUtility.ToString(row["Content_Alias"]) == string.Empty ? "" : (row["Content_Alias"].ToString() + "  ")) + ConvertUtility.FormatTimeVn(ConvertUtility.ToDateTime(row["Distribution_CreateDate"]));

                int distid = ConvertUtility.ToInt32(row["Distribution_ID"]);

                string url = BuildUrlByDistId(distid, Convert.ToInt32(GroupId));

                Image img = (Image)e.Item.FindControl("img_img");
                string src = MultimediaUtility.GetListAvatar(ConvertUtility.ToString(row["Content_Avatar"]));



                if (!string.IsNullOrEmpty(row["Content_Avatar"].ToString()) && System.IO.File.Exists(Server.MapPath(src)))
                {
                    img.ImageUrl = src;
                    img.Attributes.Add("onclick", "javascript:window.location='" + url + "'");
                    img.Attributes.Add("style", "cursor:hand");
                }
                else
                    img.Visible = false;

              //  Literal link_teaser = (Literal)e.Item.FindControl("link_teaser");
                //link_teaser.NavigateUrl = url;//BuildUrlByDistId((int)row["Content_ID"], (int)row["Distribution_ZoneID"]);
                //string teaser = ConvertUtility.ToString(row["Content_Teaser"]);

                //if (string.IsNullOrEmpty(teaser))
                //    teaser = ConvertUtility.ToString(row["Content_Body"]);

                //link_teaser.Text = teaser; //NCS.Library.Utilities.ConvertUtility.ToStringWithoutFontBold(teaser, NCS.Module.CMS.Library.Definitions.Constants.LengthLeftTeaser);

                HyperLink link1 = (HyperLink)e.Item.FindControl("link1");
                link1.Text = ConvertUtility.ToString(row["Content_Headline"]);
                link1.NavigateUrl = url;// BuildUrlByDistId((int)row["Distribution_ID"], this.GroupId);

                //HyperLink link2 = (HyperLink)e.Item.FindControl("link2");
                //link2.NavigateUrl = url;// BuildUrlByDistId((int)row["Distribution_ID"], GroupId);

                //Label lblDate = (Label)e.Item.FindControl("lblDate");
                //lblDate.Text = ConvertUtility.ToDateTime(row["Distribution_CreateDate"]).ToString("dd/MM/yyyy");

                //Label lblTime = (Label)e.Item.FindControl("lblTime");
                //lblTime.Text = ConvertUtility.ToDateTime(row["Distribution_CreateDate"]).ToString("hh:mm:ss tt");
            }
        }
    }
}